package com.wc.blue_bridge_history.Java15Q.封闭图形个数;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Comparator;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2024/4/19 17:04
 * @description https://www.dotcpp.com/oj/problem3240.html
 */
public class Main {
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 200010;
    static int[][] a = new int[N][1];
    static int[] c = new int[]{1, 0, 0, 0, 1, 0, 1, 0, 2, 1};
    static int n;

    public static void main(String[] args) {
        n = sc.nextInt();
        for (int i = 1; i <= n; i++) a[i][0] = sc.nextInt();
        Arrays.sort(a, 1, n + 1, new Comparator<int[]>() {
            public int compare(int[] o1, int[] o2) {
                int l = get(o1[0]);
                int r = get(o2[0]);
                if (l != r) return l - r;
                return o1[0] - o2[0];
            }
        });
        for(int i = 1; i <= n; i++) out.print(a[i][0] + " ");
        out.flush();
    }

    static int get(int x) {
        int res = 0;
        while (x > 0) {
            res += c[x % 10];
            x /= 10;
        }
        return res;
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}